﻿using System;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DTOSW.Core.Membership.Interfaces.Services;
using DTOSW.Core.Membership.Interfaces.Services;
using DTOSW.Core.Membership.Interfaces.Repositories;
using DTOSW.Core.Membership.Objects.Model;
using DTOSW.Core.Membership.Extensions;
using DTOSW.Core.Membership.Security;
using DTOSW.Data.Membership.Integration.Repositories;

namespace DTOSW.Service.Membership
{
    public class MembershipService : IMembershipService
    {
        private readonly MembershipRepository _membershipRepository;

        public MembershipService()
        {
            _membershipRepository = new MembershipRepository();
            if (_membershipRepository == null)
                throw new NullReferenceException("User repository cannot be null.");

            this._membershipRepository = _membershipRepository;
        }

        public List<User> GetUsers()
        {
            List<User> userList = _membershipRepository.GetUser().ToList();

            if (userList == null)
                throw new NullReferenceException("The User repository contains no members.");


            return userList;
        }
        public User GetUserByUserID(Guid userID)
        {
            User user = _membershipRepository.GetUser().WithUserID(userID);

            if (user == null)
                throw new NullReferenceException("There are no user with this User ID");

            return user;
        }
        public User GetUserByUserName(string UserName)
        {
            User user = _membershipRepository.GetUser().WithUserName(UserName);

            if (user == null)
                throw new NullReferenceException("There are no user with this User Name");

            return user;
        }
        public User GetUserByFullName(string FirstName, string LastName)
        {
            if (FirstName == string.Empty || LastName == string.Empty)
                throw new NullReferenceException("You missing some parameter");

            User user = _membershipRepository.GetUser().WithFullName(FirstName, LastName);

            if (user == null)
                throw new NullReferenceException("There are no user with this Name");

            return user;
        }

        public CreationStatus CreateUser(string username, string password, string email)
        {
            if (String.IsNullOrEmpty(username))
                return CreationStatus.InvalidUserName;

            if (String.IsNullOrEmpty(password))
                return CreationStatus.InvalidPassword;

            if (String.IsNullOrEmpty(email))
                return CreationStatus.InvalidEmail;

            if (_membershipRepository.GetUser().WithUserName(username) != null)
                return CreationStatus.DuplicateUserName;
            
            return _membershipRepository.CreateUser(username, password, email);
        }
        public DeleteStatus DeleteUser(Guid userID)
        {
            if (userID == Guid.Empty)
                return DeleteStatus.InvalidUserID;

            
            return _membershipRepository.DeleteUser(userID);
        }
        public UpdateStatus UpdateProfile(Profile profile)
        {
            if (profile == null)
                return UpdateStatus.NullException;

            if (profile.UserID == Guid.Empty)
                return UpdateStatus.InvalidUserID;

            return _membershipRepository.UpdateProfile(profile);
        }
        public bool ChangePassword(Guid userID, string currentPassword, string newPassword)
        {
            if (userID == Guid.Empty || currentPassword == null || newPassword == null)
                throw new NullReferenceException("Some arguments is null");

            _membershipRepository.ChangePassword(userID, currentPassword, newPassword);

            return true;
        }
        public UserValidateStatus ValidateUser(string username, string password)
        {
            if (String.IsNullOrEmpty(username))
               return UserValidateStatus.UsernameNullable;

            if (String.IsNullOrEmpty(password))
                return UserValidateStatus.PasswordNullable;

            return _membershipRepository.ValidateUser(username, password);
        }

    }
}
